package com.moxiao.yfjh.module.activity.service.productType;

import com.moxiao.yfjh.module.activity.dal.dataobject.merchantInfo.MerchantInfoDO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.moxiao.yfjh.module.activity.controller.admin.productType.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.productType.ProductTypeDO;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.activity.convert.productType.ProductTypeConvert;
import com.moxiao.yfjh.module.activity.dal.mysql.productType.ProductTypeMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.activity.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲商品类型 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class ProductTypeServiceImpl implements ProductTypeService {

    @Resource
    private ProductTypeMapper productTypeMapper;

    @Override
    public Long createProductType(ProductTypeCreateReqVO createReqVO) {
        // 插入
        ProductTypeDO productType = ProductTypeConvert.INSTANCE.convert(createReqVO);
        productTypeMapper.insert(productType);
        // 返回
        return productType.getId();
    }

    @Override
    public void updateProductType(ProductTypeUpdateReqVO updateReqVO) {
        // 校验存在
        validateProductTypeExists(updateReqVO.getId());
        // 更新
        ProductTypeDO updateObj = ProductTypeConvert.INSTANCE.convert(updateReqVO);
        productTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteProductType(Long id) {
        // 校验存在
        validateProductTypeExists(id);
        // 删除
        productTypeMapper.deleteById(id);
    }

    private void validateProductTypeExists(Long id) {
        if (productTypeMapper.selectById(id) == null) {
            throw exception(PRODUCT_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ProductTypeDO getProductType(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public List<ProductTypeDO> getProductTypeList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return productTypeMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ProductTypeDO> getProductTypePage(ProductTypePageReqVO pageReqVO) {
        return productTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ProductTypeDO> getProductTypeList(ProductTypeExportReqVO exportReqVO) {
        return productTypeMapper.selectList(exportReqVO);
    }

    @Override
    public ProductTypeDO getProductTypeTodayLast(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<ProductTypeDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(ProductTypeDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(ProductTypeDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return productTypeMapper.selectOne(lqw);
    }

    @Override
    public Integer getProductTypeNo(String ProductTypeNo) {
        return productTypeMapper.getProductTypeNo(ProductTypeNo);
    }

}
